﻿
#include"sellDataInfo.h"
#include "productInfo.h"
#include "simpleLog.h"
#include <algorithm>
#include <set>

std::string readFile(const std::string& filePath, std::vector<std::string>& header, std::vector<std::vector<std::string>>& data);
int findIndex(const std::vector<std::string>& header, const std::string& columnName);

namespace {
  // 商品编号	商品条码	商品全名	商品单位	数量	单价
  std::string GetOutpuTitleStr() {
    //return ProductInfo::KeyColName() + "\t" + ProductInfo::IDColName() + "\t" + ProductInfo::UnitPriceColName()
    //  + "\t" + SellDataInfo::SellNumColName();
    return "商品编号	商品条码	商品全名	商品单位	数量	单价";
  }
}

bool SellDataInfo::ReadSellInfo() {
  std::vector<std::vector<std::string>> dataOri;
  auto readResult = readFile(file1Path, header2, dataOri);
  if (!readResult.empty()) {
    logObj << readResult << file1Path << std::endl;
    return false;
  }
  return OrignzeInfo(dataOri);
}

bool SellDataInfo::OrignzeInfo(const std::vector<std::vector<std::string>>& dataOri)
{
  int purchaserNameIndex2 = findIndex(header2, SellDataInfo::PurchaserKeyColName());
  if (purchaserNameIndex2 == -1) {
    logObj << "error:售卖信息中未找到列名: " << SellDataInfo::PurchaserKeyColName() << std::endl;
    return false;
  }
  // 查找“商品名称”和“下单数量”在第二个文件中的索引
  nameIndex2 = findIndex(header2, SellDataInfo::KeyColName());
  if (nameIndex2 == -1) {
    logObj << "error:售卖信息数据中未找到列名: " << SellDataInfo::KeyColName() << std::endl;
    return false;
  }
  quantityIndex2 = findIndex(header2, SellDataInfo::SellNumColName());
  if (quantityIndex2 == -1) {
    logObj << "error:售卖信息数据中未找到" << SellDataInfo::SellNumColName() << std::endl;
    return false;
  }

  for (const auto & singleInfo : dataOri)
  {
    if (singleInfo.size() <= purchaserNameIndex2 || singleInfo.size() <= nameIndex2 || singleInfo.size() <= quantityIndex2)
    {
      //logObj << "error:数据不全。: " << std::endl;
      continue;
    }
    auto strPurchaserName = singleInfo[purchaserNameIndex2];
    data2[strPurchaserName].push_back(singleInfo);
  }
  logObj << "读取到" << data2.size()<<"家商家的数据。" << std::endl;
  return true;
}

std::string LinkString(const std::vector<std::string>& info, std::string spilit = " ");
std::string UTF8ToANSI(const std::string& utf8);


SellDataInfo::SellDataInfo(const std::string& file, Logger& log)
  :file1Path(file), logObj(log)
{
  readInfoSucc = ReadSellInfo();
}

void SellDataInfo::ParseToOutput(const std::unordered_map<std::string, std::vector<std::string>> &data,
  int IDIndex, int unitPriceIndex, int BarCodeIndex)
{
  std::string filePre = logObj.GetTimeStr();
  if (filePre.length() > 19)
  {
    std::replace(filePre.begin(), filePre.end(), ' ', '_');
    std::replace(filePre.begin(), filePre.end(), ':', '-');
    filePre = filePre.substr(0, 19);
    filePre = filePre.append("_");
  }
  std::set<std::string> names;
  for (auto iter = data2.begin(); iter != data2.end(); ++iter) {
    const std::string outputPath = filePre + UTF8ToANSI(iter->first)+".txt";
    // 写入合并后的数据到第三个文件
    std::ofstream outputFile(outputPath);
    if (!outputFile.is_open()) {
      logObj << "error：无法打开输出文件: " << outputPath << std::endl;
      continue;
    }
    outputFile << GetOutpuTitleStr() << std::endl;
    //商品编号	商品条码	商品全名	商品单位	数量	单价
    for (const auto& singleInfo : iter->second) {
      if (singleInfo.size() <= nameIndex2 || singleInfo.size() <= quantityIndex2) {
        logObj << "error:售卖信息数据不全：" << LinkString(singleInfo) << std::endl;
        continue;
      }
      auto produceName = singleInfo[nameIndex2];
      auto produceQuantity = singleInfo[quantityIndex2];
      auto iterFind = data.find(produceName);
      if (iterFind == data.end())
      {
        logObj << "error:售卖信息，商品名没找到：" << LinkString(singleInfo) << std::endl;
        names.insert(produceName);
        continue;
      }
      if (iterFind->second.size() <= unitPriceIndex || iterFind->second.size() <= IDIndex || iterFind->second.size() <= BarCodeIndex) {
        logObj << "error:商品信息不全："<< produceName << std::endl;
        continue;
      }
      outputFile << iterFind->second[IDIndex] << "\t" << iterFind->second[BarCodeIndex] << "\t"
        << produceName << "\t" << "斤"
        << "\t" << produceQuantity << "\t" << iterFind->second[unitPriceIndex] << std::endl;
    }
    outputFile.close();
  }
  if (!names.empty())
  {
    logObj << "统计有:";
    for (auto name : names)
    {
      logObj << name << "、";
    }
    logObj << "没找到商品信息" << std::endl;;
  }
}

